package com.webserver.core;

import com.webserver.annotation.Controller;
import com.webserver.annotation.RequestMapping;
import com.webserver.controller.ArticleController;
import com.webserver.controller.ToolsController;
import com.webserver.controller.UserController;
import com.webserver.http.HttpServletRequest;
import com.webserver.http.HttpServletResponse;
import sun.reflect.generics.scope.ClassScope;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URISyntaxException;

/**
 * 用于处理请求
 */
@Controller
public class DispatcherServlet {
    private static File root;
    private static File staticDir;
    static {
        try {
            root = new File(DispatcherServlet.class.getClassLoader().getResource(".").toURI());
            staticDir = new File(root,"static");
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }

    }

    public void service(HttpServletResponse response, HttpServletRequest request){

        String path = request.getRequestURI();//path: /myweb/index.html
        System.out.println("----------------------"+path);
        try {
            File controller = new File(DispatcherServlet.class.getClassLoader().getResource("./com/webserver/controller").toURI());
            File[] files = controller.listFiles(f->f.getName().endsWith(".class"));
            for (File file1 : files){
                String className = file1.getName().substring(0,file1.getName().indexOf("."));
                Class cls = Class.forName("com.webserver.controller."+className);
                if (cls.isAnnotationPresent(Controller.class)) {
                    System.out.println("---------------------111-");
                    Object obj = cls.newInstance();
                    Method[] methods = cls.getDeclaredMethods();
                    for (Method method : methods){
                        System.out.println("---------------------------22");
                        if (method.isAnnotationPresent(RequestMapping.class)){
                            System.out.println("--------------------------------------333");
                            RequestMapping arm = method.getAnnotation(RequestMapping.class);
                            if (arm.value().equals(path)){
                                System.out.println("--------------------------444");
                                method.invoke(obj,request,response);
                                return;
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        File file = new File(staticDir, path);
            System.out.println("页面是否存在: " + file.exists());
            if (file.isFile()) {
                response.setContentFile(file);

            } else {
                System.out.println("---------------------------");
                response.setStatusCode(404);
                response.setStatusReason("NotFound");
                file = new File(staticDir, "root/404.html");
                response.setContentFile(file);
            }

        }


        //测试添加一个额外的响应头
//        response.addHeader("server","WebServer");

}
